package jp.sourceforge.akjrcp.ui.param;

import jp.sourceforge.akjrcp.ui.param.AbstractParamCompositeItem.SelectionCheck;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;

public class ParamSpinner extends AbstractParamCompositeItem{

private String key;
private String label;


private Label labelItem;

private String value="";

private Spinner spinner;

public String getKey() {
	return key;
}

public String getLabel() {
	return label;
}

public void setDigits(int digit){
	spinner.setDigits(digit);
}

public void setToolTipText(String text){
	labelItem.setToolTipText(text);
	this.spinner.setToolTipText(text);
}


public double getDigitedSelection(){

	int base=1;
	for(int i=0;i<spinner.getDigits();i++){
		base*=10;
	}
	return (double)spinner.getSelection()/base;
}

public String getValue() {
	return ""+getDigitedSelection();
}


public void setValue(String value) {
	if(value.length()>0){//set valid value
		double dvalue=Double.parseDouble(value);
		spinner.setSelection(forDigit(dvalue));
	}
	this.value=value;
}
	public ParamSpinner(Composite parent, int style,String key,String label) {
		super(parent, style);
		
		this.key=key;
		this.label=label;
		
		this.setLayout(new GridLayout(2,false));
		labelItem = new Label(this,SWT.NONE);
		labelItem.setText(label);
		
		spinner = new Spinner(this,SWT.NONE);
		spinner.addSelectionListener(new SelectionCheck());
		
		//GridData gdata=new GridData();
		//gdata.widthHint=30;
		
		//spinnerAndScale.setLayoutData(gdata);
	}


	
	public boolean isModified() {
		
		String oldValue=getValue();
		
		
		
		return !oldValue.equals(value);
	}

	public void setMaximum(double max){
		spinner.setMaximum(forDigit(max));
	}
	public void setMinimum(double min){
		spinner.setMinimum(forDigit(min));
	}


	private int forDigit(double value){
		int base=1;
		for(int i=0;i<spinner.getDigits();i++){
			base*=10;
		}
		return (int) (value*base);
	}

	public Spinner getSpinner() {
		return spinner;
	}

	public void setSpinner(Spinner spinner) {
		this.spinner = spinner;
	}


}
